package com.note.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.note.api.RoleService;
import com.note.api.UserService;
import com.note.common.Const;
import com.note.common.ResponseCode;
import com.note.common.ServerResponse;
import com.note.config.AmqpConfig;
import com.note.model.Role;
import com.note.model.User;
import com.note.util.NoteUtil;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * Created by ADMIN on 2018/3/16.
 */
@RestController
@RequestMapping(value = "/user")
public class UserController extends BaseController{
    private static final Logger logger = LoggerFactory.getLogger(UserController.class);
    //注入服务提供方暴露的接口，通过@Reference注解，dubbo会在扫描的时候自动代理接口，然后通过rpc调用远程服务。
    //如果用xml配置方式，需要将@Reference换成@Autowired。
    @Reference(version = "1.0.0")
    UserService userService;

    @Reference(version = "1.0.0")
    RoleService roleService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    // 查询所有用户
    @ApiOperation(value = "查询用户", notes = "查询所有用户")
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public ServerResponse getUser(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum, @RequestParam(value = "pageSize", defaultValue = "10") int pageSize){
        ServerResponse<User> userList = userService.list(pageNum,pageSize);
        if (userList.isSuccess()) {
            rabbitTemplate.convertAndSend(AmqpConfig.QUEUE_NAME, userList.getData());
        }
        return userList;
    }

    @ApiOperation(value= "保存用户" , notes = "保存用户")
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public ServerResponse<User> saveUser(User user){
        user.setPassword(NoteUtil.md5(user.getPassword()));
        int result = userService.save(user);
        if (result > 0) {
            return ServerResponse.createBySuccess(ResponseCode.SUCCESS.getCode(),"添加用户成功");
        }
        return ServerResponse.createByErrorCodeMessage(ResponseCode.ERROR.getCode(),"添加用户失败");
    }

}
